package com.sharemarking.wa.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.OrgMember;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.dispather.DispatcherInfoDto;
import com.sharemarking.wa.platform.dto.dispather.DispatcherListDto;
import com.sharemarking.wa.platform.mapper.DispatcherMapper;
import com.sharemarking.wa.platform.mapper.MemberMapper;
import com.sharemarking.wa.platform.mapper.OrgMemberMapper;
import com.sharemarking.wa.platform.service.impl.Md5ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class DispatcherService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgMemberMapper orgMemberMapper;
    @Autowired
    private DispatcherMapper dispatcherMapper;
    @Autowired
    private Md5ServiceImpl md5Service;
    @Autowired
    private MemberMapper memberMapper;

    @Transactional(rollbackFor = Exception.class)
    public ResponseParams<?> addOrEdit() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String dispatcherId = request.getParameter("dispatcherId");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");

        if (StringUtils.isEmpty(name)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("name"));
        }
        if (StringUtils.isEmpty(mobile)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("mobile"));
        }

        if (!ObjectUtils.isEmpty(memberMapper.getByMobile(mobile))) {
            throw RequestException.create(ResponseParams.competitorError("该手机已注册！"));
        }


        Member member = new Member();
        member.setRealName(name);
        member.setMobile(mobile);
        if (StringUtils.isEmpty(dispatcherId)) {
            //插入账号表9
            if (!ObjectUtils.isEmpty(memberMapper.getByMobile(password))) {
                throw RequestException.create(ResponseParams.requiredParamsIsMissing("password"));
            }
            member.setPassword(md5Service.md5(password));
            member.setCreateDate(new Date());
            member.setRole(3);
            memberMapper.register(member);
            //插入机构用户表
            Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id", memberId)).getOrgId();
            OrgMember orgMember = new OrgMember();
            orgMember.setMemberId(member.getId());
            orgMember.setOrgId(orgId);
            orgMember.setCreateDate(new Date());
            orgMemberMapper.insert(orgMember);
        } else {
            //更新用户表就行
            member.setId(Integer.valueOf(dispatcherId));
            member.setUpdateDate(new Date());
            dispatcherMapper.updateDispatcher(member);
        }


        return ResponseParams.ok(null);
    }

    public ResponseParams<?> resetPwd() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String dispatcherId = request.getParameter("dispatcherId");

        if (StringUtils.isEmpty(dispatcherId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("dispatcherId"));
        }

        //默认6个1
        String pwd = md5Service.md5("111111");

        Member member = new Member();
        member.setId(Integer.valueOf(dispatcherId));
        member.setPassword(pwd);
        member.setUpdateDate(new Date());
        //更新
        dispatcherMapper.resetPwd(member);


        return ResponseParams.ok(null);
    }

    public ResponseParams<?> getList() {
        Integer memberId = ss.getCurrentUserId();

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));
        Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id", memberId)).getOrgId();

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);
        params.put("orgId", orgId);


        List<DispatcherListDto> list = dispatcherMapper.getDispatcherList(params);


        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, dispatcherMapper.getDispatcherListTotal(params), list.size() == pageSize));
    }

    public ResponseParams<?> getInfo() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String dispatcherId = request.getParameter("dispatcherId");

        if (StringUtils.isEmpty(dispatcherId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("dispatcherId"));
        }

        DispatcherInfoDto dispatcherInfoDto = dispatcherMapper.getDispatcherInfo(Integer.valueOf(dispatcherId));

        return ResponseParams.ok(dispatcherInfoDto);
    }

    public ResponseParams<?> delete() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String dispatcherId = request.getParameter("dispatcherId");

        if (StringUtils.isEmpty(dispatcherId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("dispatcherId"));
        }
        dispatcherMapper.delete(Integer.valueOf(dispatcherId));

        return ResponseParams.ok(null);
    }

}
